package com.tm.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.tm.ResultEntity;
import com.tm.mapper.EcMemberInfoMapper;
import com.tm.mapper.EcStoreManageMapper;
import com.tm.model.EcMemberInfo;
import com.tm.model.EcStoreManage;
import com.tm.model.EcStoreManageCriteria;
import com.tm.util.DateUtilsEx;

/**
 * 会员相关接口服务类
 * @author GXL
 *
 */

@Service
public class EcMemberInfoService {
	
	@Autowired
	private EcMemberInfoMapper emiMapper;
	
	@Autowired
	private CommonService cService;
	
	@Autowired
	private EcStoreManageMapper esmMapper;

	public ResultEntity updateMember(EcMemberInfo tm) {
		ResultEntity re = null;
		try{
			
			int rei = emiMapper.updateByPrimaryKeySelective(tm);
			
			if(rei > 0){
				re = new ResultEntity("0");
			}else{
				re = new ResultEntity("-1", "修改个人信息失败！");
			}
		}catch(Exception e){
			e.printStackTrace();
			re = new ResultEntity("-1", "系统异常！");
		}
		return re;
		
	}

	public ResultEntity addMember(EcMemberInfo tm) {
		ResultEntity re = null;
		try{
			int rei = emiMapper.insertSelective(tm);
			
			if(rei > 0){
				re = new ResultEntity("0");
			}else{
				re = new ResultEntity("-1", "新增个人信息失败！");
			}
		}catch(Exception e){
			e.printStackTrace();
			re = new ResultEntity("-1", "系统异常！");
		}
		return re;
	}

	public EcMemberInfo getMemberByMobile(String mobile) {
		return emiMapper.getMemberByMobile(mobile);
	}

	public EcMemberInfo getMemberByUnionId(String unionid) {
		return emiMapper.getMemberByUnionId(unionid);
	}

	public EcMemberInfo getmemberByMemberId(Integer memberId) {
		return emiMapper.selectByPrimaryKey(memberId);
	}

	/**
	 * 获取会员收货地址列表
	 * @param memberId
	 * @return
	 */
	public List<Map<String, Object>> getAddressListByMemberId(Integer memberId) {
		return emiMapper.getAddressListByMemberId(memberId);
	}

	/**
	 * 设置默认地址
	 * @param addrId
	 * @param memberId
	 * @return
	 */
	public ResultEntity setAddr(Integer addrId, Integer memberId) {
		ResultEntity re = null;
		try{
			
			//设置该会员所有的地址为非默认
			emiMapper.cancelDefaultByMemberId(memberId);
			
			//重新设置新的默认地址
			Map<String, Object> param = new HashMap<String, Object>();
			param.put("addrId", addrId);
			param.put("memberId", memberId);
			
			int rei = emiMapper.setDefaultByMemberId(param);
			
			if(rei > 0){
				re = new ResultEntity("0");
			}else{
				re = new ResultEntity("-1", "设置失败！");
			}
			
		}catch(Exception e){
			e.printStackTrace();
			re = new ResultEntity("-1", "系统异常！");
		}
		return re;
	}

	/**
	 * 删除地址
	 * @param addrId
	 * @param memberId
	 * @return
	 */
	public ResultEntity delAddr(Integer addrId, Integer memberId) {
		ResultEntity re = null;
		try{
			
			//重新设置新的默认地址
			Map<String, Object> param = new HashMap<String, Object>();
			param.put("addrId", addrId);
			param.put("memberId", memberId);
			
			int rei = emiMapper.delAddr(param);
			
			if(rei > 0){
				re = new ResultEntity("0");
			}else{
				re = new ResultEntity("-1", "设置失败！");
			}
			
		}catch(Exception e){
			e.printStackTrace();
			re = new ResultEntity("-1", "系统异常！");
		}
		return re;
	}

	/**
	 * 获取地址详情
	 * @param addrId
	 * @return
	 */
	public Map<String, Object> getAddressByAddrId(Integer addrId) {
		return emiMapper.getAddressByAddrId(addrId);
	}

	/**
	 * 新增收货地址
	 * @param deliveryReceiver
	 * @param deliveryMobile
	 * @param deliveryAddress
	 * @param provinceId
	 * @param cityId
	 * @param areaId
	 * @param memberId
	 * @return
	 */
	public ResultEntity addAddr(String deliveryReceiver, String deliveryMobile, String deliveryAddress,
			Integer provinceId, Integer cityId, Integer areaId, Integer memberId, String isDefault) {
		ResultEntity re = null;
		try{
			
			if("Y".equals(isDefault)){
				//设置该会员所有的地址为非默认
				emiMapper.cancelDefaultByMemberId(memberId);
			}else{
				//判断会员是否有收货地址 如果没有 默认设置该地址为默认地址
				int i = emiMapper.getAddressListByMemberId(memberId).size();
				if(i == 0){
					isDefault = "Y";
				}
			}
			
			//重新设置新的默认地址
			Map<String, Object> param = new HashMap<String, Object>();
			param.put("deliveryReceiver", deliveryReceiver);
			param.put("deliveryMobile", deliveryMobile);
			param.put("deliveryAddress", deliveryAddress);
			param.put("provinceId", provinceId);
			param.put("cityId", cityId);
			param.put("memberId", memberId);
			param.put("areaId", areaId);
			param.put("isDefault", isDefault);
			
			int rei = emiMapper.addAddr(param);
			
			if(rei > 0){
				re = new ResultEntity("0");
			}else{
				re = new ResultEntity("-1", "新增失败！");
			}
			
		}catch(Exception e){
			e.printStackTrace();
			re = new ResultEntity("-1", "系统异常！");
		}
		return re;
	}

	/**
	 * 编辑地址
	 * @param addrId
	 * @param deliveryReceiver
	 * @param deliveryMobile
	 * @param deliveryAddress
	 * @param provinceId
	 * @param cityId
	 * @param areaId
	 * @param memberId
	 * @param isDefault
	 * @return
	 */
	public ResultEntity updateAddr(Integer addrId, String deliveryReceiver, String deliveryMobile,
			String deliveryAddress, Integer provinceId, Integer cityId, Integer areaId, Integer memberId,
			String isDefault) {
		ResultEntity re = null;
		try{
			
			if("Y".equals(isDefault)){
				//设置该会员所有的地址为非默认
				emiMapper.cancelDefaultByMemberId(memberId);
			}
			
			//重新设置新的默认地址
			Map<String, Object> param = new HashMap<String, Object>();
			param.put("addrId", addrId);
			param.put("deliveryReceiver", deliveryReceiver);
			param.put("deliveryMobile", deliveryMobile);
			param.put("deliveryAddress", deliveryAddress);
			param.put("provinceId", provinceId);
			param.put("cityId", cityId);
			param.put("memberId", memberId);
			param.put("areaId", areaId);
			param.put("isDefault", isDefault);
			
			int rei = emiMapper.updateAddr(param);
			
			if(rei > 0){
				re = new ResultEntity("0");
			}else{
				re = new ResultEntity("-1", "新增失败！");
			}
			
		}catch(Exception e){
			e.printStackTrace();
			re = new ResultEntity("-1", "系统异常！");
		}
		return re;
	}

	/**
	 * 获取会员门店信息
	 * @param memberId
	 * @return
	 */
	public EcStoreManage getStoreByMemberId(Integer memberId) {
		EcStoreManageCriteria example = new EcStoreManageCriteria();
		example.createCriteria().andMemberInfoIdEqualTo(memberId).andIsDeleteEqualTo("N");
		List<EcStoreManage> list = esmMapper.selectByExample(example);
		if(list.size() > 0)
			return list.get(0);
		else
			return null;
	}

	/**
	 * 获取发展客户列表
	 * @param memberId
	 * @param startIndex
	 * @param pageSize
	 * @return
	 */
	public ResultEntity getLowerListByMemberId(Integer memberId, Integer startIndex, Integer pageSize) {
		ResultEntity re = null;
		try{
			
			Map<String, Object> param = new HashMap<String, Object>();
			param.put("memberId", memberId);
			param.put("startIndex", startIndex);
			param.put("pageSize", pageSize);
			
			List<Map<String, Object>> list = emiMapper.getLowerListByMemberIdWithPage(param);
			
			re = new ResultEntity(list);
			
		}catch(Exception e){
			e.printStackTrace();
			re = new ResultEntity("-1", "系统异常！");
		}
		return re;
	}

	/**
	 * 获取会员默认地址
	 * @param memberId
	 * @return
	 */
	public Map<String, Object> getDefaultAddressByMemberId(Integer memberId) {
		return emiMapper.getDefaultAddressByMemberId(memberId);
	}

	/**
	 * 签到有礼
	 * @param memberId 会员ID
	 * @return
	 */
	public ResultEntity getMemberSign(Integer memberId) {
		ResultEntity re = null;
		try{
			Map<String, Object> res = new HashMap<String, Object>();
			
			//获取会员金币余额以及连续签到天数
			EcMemberInfo member = emiMapper.selectByPrimaryKey(memberId);
			res.put("integral", member.getIntegral());
			
			//判断会员当天是否已签到以及获取当日签到获取的金币数量（获取当天签到记录）
			Map<String, Object> signToday = emiMapper.getSignLogToday(memberId);
			res.put("signToday", signToday);
			
			//获取前一天签到记录
			Map<String, Object> signYestoday = emiMapper.getSignLogYestoday(memberId);
			if(signYestoday == null){
				if(signToday != null)
					res.put("signNum", 1);
				else
					res.put("signNum", 0);
			}else{
				res.put("signNum", member.getSignNum());
			}
			
			//获取会员当月签到记录
			List<Map<String, Object>> signMonth = emiMapper.getSignLogThisMonth(memberId);
			List<Integer> dayNumList = new ArrayList<Integer>();
			for (Map<String, Object> map : signMonth) {
				dayNumList.add(Integer.parseInt(map.get("dy").toString()) - 1);
			}
			
			res.put("signMonth", signMonth);
			res.put("dayNumList", dayNumList);
			
			re = new ResultEntity(res);
			
		}catch(Exception e){
			e.printStackTrace();
			re = new ResultEntity("-1", "系统异常！");
		}
		
		return re;
	}

	/**
	 * 会员签到
	 * @param memberId 签到会员ID
	 * @return
	 */
	public ResultEntity addMemberSignLog(Integer memberId) {
		ResultEntity re = null;
		try{
			
			Map<String, Object> param = new HashMap<String, Object>();
			param.put("ii_member_id", memberId);
			param.put("oi_code", -1);
			param.put("oc_text", "");
			
			emiMapper.addMemberSignLog(param);
			
			if(!"-1".equals(String.valueOf(param.get("oi_code")))){
				re = new ResultEntity(param.get("oi_code"));
			}else
				re = new ResultEntity(String.valueOf(param.get("oi_code")), String.valueOf(param.get("oc_text")));
			
		}catch(Exception e){
			e.printStackTrace();
			re = new ResultEntity("-1", "系统异常！");
		}
		
		return re;
	}

	/**
	 * 设置会员类型
	 * @param memberId
	 * @param xjMemberId
	 * @param type
	 * @return
	 */
	public ResultEntity setMemberType(Integer memberId, String xjMemberId, String type) {
		ResultEntity re = null;
		try{
			
			//获取会员基本信息
			EcMemberInfo member = emiMapper.selectByPrimaryKey(Integer.parseInt(xjMemberId));
			
			member.setMemberType(type);
			member.setEditTime(DateUtilsEx.formatToString(new Date(), DateUtilsEx.DATE_FORMAT_SEC));
			member.setEditUserId(memberId);
			
			int rei = emiMapper.updateByPrimaryKeySelective(member);
			
			if(rei > 0){
				re = new ResultEntity("0");
			}else{
				re = new ResultEntity("-1", "操作失败！");
			}
			
		}catch(Exception e){
			e.printStackTrace();
			re = new ResultEntity("-1", "系统异常！");
		}
		
		return re;
	}

}
